package models

import (
	common "mpps/tools/common"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Resp struct {
	Code int         `json:"code"`           // 返回状态码
	Msg  string      `json:"msg,omitempty"`  // 错误信息，为空则忽略
	Data interface{} `json:"data,omitempty"` // 数据，为空则忽略
}

func WriteHttpSucc(c *gin.Context, resp interface{}) {
	c.JSON(http.StatusOK, &Resp{Code: 0, Data: resp})
}

func WriteHttpCodeSucc(c *gin.Context, err common.Errno) {
	c.JSON(http.StatusOK, &Resp{Code: err.ErrCode(), Data: err.ErrMsg()})
}

func WriteHttpFail(c *gin.Context, err common.Errno) {
	c.JSON(http.StatusOK, &Resp{Code: err.ErrCode(), Msg: err.ErrMsg()})
}

func WriteHttpCodeFail(c *gin.Context, code int, err string) {
	c.JSON(http.StatusOK, &Resp{Code: code, Msg: err})
}
